/** 
    * @name objChangeTwo
    * @params {String} str 传入被字符串包裹的对象
    * @return 将被包裹字符串的对象还原
   */
const strToObject = (str) => {
    let str1 = str.replace(/\"/g, "'");
    let str2 = str1.replace(/\“/g, '"');

    return JSON.parse(str2)
}
/** 
    * @name objectToSearh
    * @params {Object} obj 传入url参数对象
    * @return 拼接适合小程序url传参的str类型
   */
const objectToSearh = (obj = {}) => {
    let res = ''
    if (Object.prototype.toString.call(obj) === '[object Object]') {
        for (let key in obj) {
            res += `&${key}=${JSON.stringify(obj[key])}`
        }
        res = '?' + res.slice(1)
    }
    return res
}
/**
 * @name richImg
 * @param {String} str 带有img标签的富文本
 * @return 处理后的img自适应标签
 */
const richImg = (str = "") => {
    const res = str.replace(/\<img/g, '<img style="width:100%;height:auto;display:block"');
    return res
}

/**
 * @name showToastUtil
 * @param {String} str 弹窗文本
 * @return 对微信showToast弹窗根据情况优化
 */
const showToastUtil = (str = "", func = () => { }) => {
    wx.showToast({
        title: str,
        // icon: need ? (type ? 'success' : 'error') : 'none',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: true,
        complete: () => {
            //回调
            func()
        }
    });
    // wx.hideToast();

}

/**
 * @name setStorage
 * @param {String} key 缓存的键值
 * @param {String/Object/Number/Array} data 缓存的数据
 * @return 将数据缓存到本地
 */
const setStorage = (key, data) => {
    wx.setStorage({
        key,
        data
    });
}

/**
 * @name getStorage
 * @param {String} key 缓存的键值
 * @return 将数据从本地读取
 */
const getStorage = (key) => {
    // return wx.getStorage(key);
    return wx.getStorageSync(key);

}
/**
 * @name phoneReg
 * @param {Number} number 手机号码
 * @return 检验手机号是否符合规范
 */
const phoneReg = (number) => {
    //校验11位手机号
    const reg = /^1(3|4|5|6|7|8|9)\d{9}$/
    return reg.test(number)
}
/**
 * @name numReg
 * @param {Number} number 数字或其他
 * @return 检验是否纯数字
 */
const numReg = (number) => {
    const reg = /^[0-9]*$/
    return reg.test(number)
}
/**
 * @name hidePhone
 * @param {Number} mobile 11位电话号码
 * @return 中间四位加密后的号码
 */
const hidePhone = (mobile) => {
    const mobileLeft = mobile.slice(0, 3)
    const mobileRight = mobile.slice(7)
    return mobileLeft + '****' + mobileRight
}
export {
    strToObject,
    objectToSearh,
    richImg,
    showToastUtil,
    setStorage,
    getStorage,
    phoneReg,
    numReg,
    hidePhone
}